<?php

/**
 * This is the model class for table "{{settings}}".
 *
 * The followings are the available columns in table '{{settings}}':
 * @property integer $set_id
 * @property string $set_key
 * @property string $set_value
 * @property string $set_remark
 * @property string $set_ckey
 */
class Settings extends CActiveRecord
{
	const SETTINGS_CKEY = 'Csystem';	//定义 一个常量，用于区分是否为系统设置[ckey]
	/**
	 * Returns the static model of the specified AR class.
	 * @return Settings the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{settings}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('set_key, set_ckey', 'required'),
			array('set_key', 'length', 'max'=>50),
			array('set_ckey', 'length', 'max'=>100),
			array('set_value, set_remark', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('set_id, set_key, set_value, set_remark, set_ckey', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'set_id' => Yii::t('settings','ID'),
			'set_key' => Yii::t('settings','Key'),
			'set_value' => Yii::t('settings', 'Value'),
			'set_remark' => Yii::t('settings', 'Remark'),
			'set_ckey' => Yii::t('settings', 'Category'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('set_id',$this->set_id);
		$criteria->compare('set_key',$this->set_key,true);
		$criteria->compare('set_value',$this->set_value,true);
		$criteria->compare('set_remark',$this->set_remark,true);
		$criteria->compare('set_ckey',$this->set_ckey,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function addOptions($key,$name='',$value='',$ckey='',$remark='')
	{
		$sitename = self::model()->find(array(
			'condition'=>'set_key = :set_key',
			'params'=>array(
				':set_key'=>$key
			)
		));
		if(!$sitename)
		{
			$model = new Settings();
			$model->set_key = $key;
			$model->set_name = $name;
			$model->set_value =  $value;
			$model->set_ckey = $ckey;
			$model->set_remark = $remark;
			$model->save();			
		}
		
	}
	

	public function getItemByKey($key)
	{
		if(Yii::app()->fileCache->get(__CLASS__.$key) == false)
		{
			$result =  self::model()->find(array(
				'condition'=>'set_key = :set_key AND set_ckey = :set_ckey',
				'params'=>array(
					':set_key'=>$key,
					':set_ckey'=>self::SETTINGS_CKEY
				)	
			));

			Yii::app()->fileCache->set(__CLASS__.$key,$result,3600,new CDbCacheDependency("SELECT * FROM {{settings}} WHERE set_key = '".$key."'"));
		}

		return Yii::app()->fileCache->get(__CLASS__.$key);		
		

	}
	
	
	public function getKeyword()
	{
		if ($this->getItemByKey('siteKeyWord'))
			return $this->getItemByKey('siteKeyword')->set_value;
		else
			return Yii::app()->params->keyword;
	}
	
	public function getDescription()
	{
		if ($this->getItemByKey('siteDescription'))
			return $this->getItemByKey('siteDescription')->set_value;
		else
			return Yii::app()->params->description;
	}
	
	public function getAuthor()
	{
		return Yii::app()->params->author;
	}
	
	public function getEditor()
	{
		return Yii::app()->params->editor;
	}
		
}